package com.huluobo.a2307douyindemo.login

import androidx.lifecycle.MutableLiveData
import com.huluobo.lib_base.bean.Money
import com.huluobo.lib_base.bean.UserBean
import com.huluobo.lib_base.db.DouYinDatabase
import com.huluobo.lib_base.mvvm.BaseViewModel
import com.huluobo.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import okhttp3.RequestBody
import javax.inject.Inject

/**
 *  Created by LC on 2025/9/15.
 */
@HiltViewModel
class LoginViewModel @Inject constructor(val apiService: ApiService, val db: DouYinDatabase) : BaseViewModel() {
    //登录成功的LiveData
    val loginSuccess by lazy { MutableLiveData<UserBean>() }

    fun login(body: RequestBody) {
        executeNetwork(
            { apiService.login(body) },
            loginSuccess::postValue
            //上下两个成功的方法是等价的
//            {
//                loginSuccess.postValue(it)
//            }
        )
    }

    val insertMoneySuccess by lazy { MutableLiveData<String>() }
    fun insertMoney(money: Money) {
        executeDb(
            {
                db.getMoneyDao().insertMoney(money)
                "添加余额成功"
            },
            insertMoneySuccess::postValue
        )
    }
}